package com.trs.dao;

import java.util.Date;
import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.trs.entry.DocJson;

public interface DocJsonDao extends JpaRepository<DocJson, Integer> {

	DocJson findByPathAndServerid(String path, Integer serverid);

	@Modifying(clearAutomatically = true)
	@Transactional
	@Query(nativeQuery = true, value = "update cqdocjson set docid=? where id=?")
	Integer updateDocid(Integer docid, Integer id);

	@Modifying
	@Transactional
	@Query(nativeQuery = true, value = "update cqdocjson set issend=? where id=?")
	Integer updateIsSend(Integer issend, Integer id);

	@Modifying
	@Transactional
	@Query(nativeQuery = true, value = "update cqdocjson set issend=? where issend=?")
	Integer updateRetry(Integer issend, Integer id);

	@Query(nativeQuery = true, value = "select * from cqdocjson where issend<0")
	List<DocJson> findByRetryList();

	@Query(nativeQuery = true, value = "select max(id) from cqdocjson where createtime<?")
	Integer findByDate(Date date);

	@Query(nativeQuery = true, value = "select distinct (substr(createtime,1,10)) from cqdocjson where createtime<?")
	List<String> findDate(Date date);

	@Transactional
	@Modifying
	@Query(nativeQuery = true, value = "delete from cqdocjson where id<=?")
	Integer deleteByIdless(Integer id);
}
